摘要:
Maximum Depth of Binary Tree2014.1.7 23:26Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.Solution: depth(tree) = max(depth(left subtree), depth(right subtree)) + 1; depth(nullptr) = 0; T... 阅读全文
摘要:
Binary Tree Zigzag Level Order Traversal2014.1.1 02:13Given a binary tree, return thezigzag level ordertraversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 ... 阅读全文
摘要:
Binary Tree Level Order Traversal2014.1.1 01:53Given a binary tree, return thelevel ordertraversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its level order traversal as:[ [3], [9,20], [1... 阅读全文
摘要:
Symmetric Tree2014.1.1 01:12Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the following is not: 1 / \ 2 2 \ \ 3 3Solution: Well..., I guess it's the new... 阅读全文
摘要:
Same Tree2013.12.31 22:56Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.Solution: Check the root, the left subtree and the right subtree, recursively. That's the 阅读全文
摘要:
Recover Binary Search Tree2013.12.31 19:00Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?confused what"{1,#,2,3}"means? 阅读全文
摘要:
Validate Binary Search Tree2013.12.31 18:48Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keysless thanthe node's key.The right subtree of a node contains only nodes with keysgreater 阅读全文
摘要:
Unique Binary Search Trees2013.12.31 18:37Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n?For example,Givenn= 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / ... 阅读全文
摘要:
Binary Tree Inorder Traversal2013.12.31 18:26Given a binary tree, return theinordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,3,2].Note:Recursive solution is trivial, could you do it iteratively?confused what"{1,#,2,3}"means?> read more 阅读全文
摘要:
Restore IP Addresses2013.12.31 18:06Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given"25525511135",return["255.255.11.135", "255.255.111.35"]. (Order does not matter)Solution: Given a number strin 阅读全文