100. ๊ฐ์ ๋๋ฌด ๐
์๋ฃจ์ ๊ฐ๋ฐ:
์ง๋ฌธ
์ด ๊ธฐ์ฌ์์๋ Leetcode์ '100. Same Tree' ์ง๋ฌธ์ ๋ค๋ฃฐ ๊ฒ์ ๋๋ค. ์ด ์ง๋ฌธ์ ์ฌ์ด ์ง๋ฌธ์ผ๋ก ํ๊ฐ๋ฉ๋๋ค.
์๋ฌธ:
Given the roots of two binary trees
p
andq
, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
์์:
1 1
/ \ / \
2 3 2 3
Input: p = [1,2,3], q = [1,2,3]
Output: true
์ง๋ฌธ ์ค๋ช
์ฐ๋ฆฌ๊ฐ ์๊ตฌํ๋ ๊ฒ์ ๋ ์ด์ง ํธ๋ฆฌ๊ฐ ๋์ผํ์ง ์ฌ๋ถ๋ฅผ ํ์ธํ๋ ๊ฒ์ ๋๋ค. ๋ ํธ๋ฆฌ์์
nodes
์ ๋ชจ๋ ๊ฐ์ ๋์ผํ ์์๋ก ๋น๊ตํ ๊ฒ์ผ๋ก ์์๋ฉ๋๋ค.๊ถ์ฅ ์ง์
์ฐ๋ฆฌ๋ ๋ฌด์์ ์๊ณ ์์ต๋๊น?
์ด๋ป๊ฒ ํ ๊ฒ์ธ๊ฐ:
left
๋๋ right
ํฌ์ธํฐnull
๊ฐ ์์ต๋๊น? ๋ฏธ๋ฌ ๋
ธ๋๊ฐ null์ด ์๋์ ์๋ฏธํฉ๋๊น? ์ด ๊ฒฝ์ฐ ์๋ชป๋ ํธ๋ฆฌ์
๋๋ค. ๋น ์ค ํ๊ธฐ๋ฒ:
๋ฆฌํธ์ฝ๋ ๊ฒฐ๊ณผ:
์ ์ถ ๋งํฌ ์ฐธ์กฐ:
ํด๊ฒฐ์ฑ
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} p
* @param {TreeNode} q
* @return {boolean}
*/
var isSameTree = function (p, q) {
/* -------------------------------------------------------------------------- */
/* 100. Same Tree */
/* -------------------------------------------------------------------------- */
/**
* @author Samuel Hinchliffe
* @see {@link linkedin.com/in/samuel-hinchliffe-๐-2bb5801a5/ | Author's Linkedin }
* @see {@link github.com/Samuel-Hinchliffe}
*/
/* ----------------------------- Solution Below ----------------------------- */
// So both our trees current node is null
// This mean's they both reached the end of the tree
// at the same time without error
if (p == null && q == null) {
return true;
}
// One of the pointers are null when another is not
// This mean's one of our pointers has traversed to a correct node
// but another has reached the end of the list too early and thus
// cannot be a valid mirror tree
if ((p == null && q != null) || (q == null && p != null)) {
return false;
}
// As we have moved nodes
// Are they the same value?
if (p.val != q.val) {
return false;
}
// Get both left nodes
// We will traverse the left nodes in a DFS fashion
// to be able to compare both left nodes at the same time
// So we move left at the same time on both trees.
let good_lefts = isSameTree(p.left, q.left);
// Get both right nodes
// We will traverse the right nodes in a DFS fashion
// to be able to compare both right nodes at the same time
// So we move right at the same time on both trees.
let good_rights = isSameTree(p.right, q.right);
// So are both sides good?
return good_lefts && good_rights;
};
Reference
์ด ๋ฌธ์ ์ ๊ดํ์ฌ(100. ๊ฐ์ ๋๋ฌด ๐), ์ฐ๋ฆฌ๋ ์ด๊ณณ์์ ๋ ๋ง์ ์๋ฃ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๋ณด์๋ค
https://dev.to/samuelhinchliffe/100-same-tree-5b4p
ํ
์คํธ๋ฅผ ์์ ๋กญ๊ฒ ๊ณต์ ํ๊ฑฐ๋ ๋ณต์ฌํ ์ ์์ต๋๋ค.ํ์ง๋ง ์ด ๋ฌธ์์ URL์ ์ฐธ์กฐ URL๋ก ๋จ๊ฒจ ๋์ญ์์ค.
์ฐ์ํ ๊ฐ๋ฐ์ ์ฝํ
์ธ ๋ฐ๊ฒฌ์ ์ ๋
(Collection and Share based on the CC Protocol.)
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} p
* @param {TreeNode} q
* @return {boolean}
*/
var isSameTree = function (p, q) {
/* -------------------------------------------------------------------------- */
/* 100. Same Tree */
/* -------------------------------------------------------------------------- */
/**
* @author Samuel Hinchliffe
* @see {@link linkedin.com/in/samuel-hinchliffe-๐-2bb5801a5/ | Author's Linkedin }
* @see {@link github.com/Samuel-Hinchliffe}
*/
/* ----------------------------- Solution Below ----------------------------- */
// So both our trees current node is null
// This mean's they both reached the end of the tree
// at the same time without error
if (p == null && q == null) {
return true;
}
// One of the pointers are null when another is not
// This mean's one of our pointers has traversed to a correct node
// but another has reached the end of the list too early and thus
// cannot be a valid mirror tree
if ((p == null && q != null) || (q == null && p != null)) {
return false;
}
// As we have moved nodes
// Are they the same value?
if (p.val != q.val) {
return false;
}
// Get both left nodes
// We will traverse the left nodes in a DFS fashion
// to be able to compare both left nodes at the same time
// So we move left at the same time on both trees.
let good_lefts = isSameTree(p.left, q.left);
// Get both right nodes
// We will traverse the right nodes in a DFS fashion
// to be able to compare both right nodes at the same time
// So we move right at the same time on both trees.
let good_rights = isSameTree(p.right, q.right);
// So are both sides good?
return good_lefts && good_rights;
};
Reference
์ด ๋ฌธ์ ์ ๊ดํ์ฌ(100. ๊ฐ์ ๋๋ฌด ๐), ์ฐ๋ฆฌ๋ ์ด๊ณณ์์ ๋ ๋ง์ ์๋ฃ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๋ณด์๋ค https://dev.to/samuelhinchliffe/100-same-tree-5b4pํ ์คํธ๋ฅผ ์์ ๋กญ๊ฒ ๊ณต์ ํ๊ฑฐ๋ ๋ณต์ฌํ ์ ์์ต๋๋ค.ํ์ง๋ง ์ด ๋ฌธ์์ URL์ ์ฐธ์กฐ URL๋ก ๋จ๊ฒจ ๋์ญ์์ค.
์ฐ์ํ ๊ฐ๋ฐ์ ์ฝํ ์ธ ๋ฐ๊ฒฌ์ ์ ๋ (Collection and Share based on the CC Protocol.)
์ข์ ์นํ์ด์ง ์ฆ๊ฒจ์ฐพ๊ธฐ
๊ฐ๋ฐ์ ์ฐ์ ์ฌ์ดํธ ์์ง
๊ฐ๋ฐ์๊ฐ ์์์ผ ํ ํ์ ์ฌ์ดํธ 100์ ์ถ์ฒ ์ฐ๋ฆฌ๋ ๋น์ ์ ์ํด 100๊ฐ์ ์์ฃผ ์ฌ์ฉํ๋ ๊ฐ๋ฐ์ ํ์ต ์ฌ์ดํธ๋ฅผ ์ ๋ฆฌํ์ต๋๋ค