My Blog List
Showing posts with label Leetcode_tree. Show all posts
Showing posts with label Leetcode_tree. Show all posts
[Leetcode Solution] Binary Tree Inorder Traversal
Analysis
- It's pretty simply by using recursive way to do in-order traversal of the binary tree
- There are two ways to do it iteratively
- First, using a stack dfs with a state variable for each node to indicate if the left child has been visited. If it is ,then visit this node and then visit the right child sub tree. Else if the left child sub tree has not been visited, set the state variable as visited, and then visit left child sub tree first
- Second traversal iteratively with constant space by using Morris in-order traversal which is a pretty tricky solution. It maintains the traversal order by delicate modify the tree and recover the tree back after traverse. The details can be found here
Note
- If using a stack to store the traversal sequence of DFS, attention should be paid if the back of stack if frequently modified. Because after pop_back or push_back, the stack.back() might not the ones you expected
Code
[Leetcode Solution] Unique Binary Search Trees II
GREAT PROBLEM
Analysis
- Using the same idea with the previous problem "Unique Binary Search Tree" this problem could be solved
- Let f[n] denote all of the root node of the unique binary search tree with n nodes, then f[n] is a combination of all of the root as the value of i-th element with all of the possible sub-tree
Note
Code
[Leetcode Solution] Unique Binary Search Trees
GOOD PROBLEM
Analysis
- Good problem with tricky idea behind it. Once read this problem one year ago at data structure course but a specific size of n which I solved by handy building up all the unique trees. Today focus this problem I still didn't figure out the solution myself.
- Build up the binary tree with the same array of numbers but in different order will resulting into different trees.
- Given a sequence with n elements in ascending order to build the binary tree. If we choose i-th elements as the root of the tree, then the problem will be reduced into two sub-problems which aim build up a binary tree with a sequence in ascending order with length of i-1 and n-i separately. This is the key trick of this problem which implies us solving the problem by dynamic programming without doubt
- Let f[n] denote the number of unique binary trees can be built with a sequence with length of n. Then
- f[n] = f[i-1]*f[n-i] (0<=i<=n)
Note
Code
[Leetcode Solution] Validate Binary Search Tree
Analysis
- Idea is straightforward by using the property that inorder traversal of binary search tree is an ascending sequence
Note
- Attention should be paid that when we use INT_MIN as the smallest element as s specific value, some element in the input might be INT_MIN
Code
[Leetcode Solution] Recover Binary Search Tree
GOOD PROBLEM
Analysis
- A inorder traversal of binary search tree results into a ascending order sequence
- Thus a solution using O(n)space is straightforward. By performing inorder traversal and getting the ascending order sequence, it would be quite easy to find out the swapped elements
- A solution using constant space is quite tricky because whether using recursion, the normal inorder traversal needs at least O(logn) space to store the searching status
- The trick is constant inorder traversal found here call Morris In-order Traversal using Threading
- After having constant space traversal method, we check each element while traversal if it is in a correct ascending order
- Another trick is how to deal with the situation that the swapped two elements are adjacent ones
Note
- In-order traversal binary tree in constant space
Code
[Leetcode Solution] Same Tree
Analysis
- Using recursion
- If the value of the root node of two trees are same, and the two pair of subtrees are same, then the two tree are the same.
Note
Code
[Leetcode Solution] Symmetric Tree
Analysis
- Using BFS search each layer of binary tree from the root to leaf
- Validate if elements in each layer is symmetric
- If all the layers are symmetric, then so is the tree
Note
Code
[Leetcode Solution] Binary Tree Level Order Traversal
Analysis
- Using BFS search each layer of binary tree from the root to leaf
- Print out nodes in each layer
Note
Code
[Leetcode Solution] Binary Tree Zigzag Level Order Traversal
Analysis
- Using BFS search each layer of binary tree from the root to leaf
- When push back nodes in each layer to the result vector, push back them alternatively in order and reversed order
Note
Code
[Leetcode Solution] Maximum Depth of Binary Tree
Analysis
- Using BFS search each layer of binary tree from the root to leaf and the node with biggest layer number is the depth of tree
- Directly recursive call the function on both sub tree and return the max result plus one. Found code here
Note
Code
[Leetcode Solution] Construct Binary Tree from Preorder and Inorder Traversal
Analysis
- Almost the same as previous problem
Note
Code
[Leetcode Solution] Construct Binary Tree from Inorder and Postorder Traversal
Analysis
- Inorder traversal is a traversal scheme that first visit the left child node and then visit the parent node and finally visit the right node
- Postorder traversal is visit the parent node and last
- Given inorder traversal sequence and postorder traversal sequence, we can first find out the root node which stored at the end of postorder traversal sequence
- Then according to finding the position of root node in the inorder traversal sequence, we can separate the sequence into two parts which are nodes belonging to left sub tree and right sub tree separately.
- Have the length of each sub tree, we could also extract their postorder traversal sequence at the postorder traversal sequence of root node
- At this point, we can build the two sub tree recursively and then point the root node child pointers to them.
- The recursion termination condition is when the traversal sequence is empty
Note
Code
[Leetcode Solution] Binary Tree Level Order Traversal II
Analysis
- Perform BFS on the binary tree layer by layer stored by two vector alternatively
- Print the nodes within one layer separatively
Note
Code
[Leetcode Solution] Convert Sorted Array to Binary Search Tree
Analysis
- Using recursion build up the sub tree then build up the tree
Note
Code
[Leetcode Solution] Convert Sorted List to Binary Search Tree
Analysis
- Idea by using recursion.
- For converting a sorted list to binary search tree, firstly find the middle item in the list.
- Then recursively convert the first half list and second half list to two binary search tree separately.
- Finally create a new node with value of the middle item and each child pointer pointing to the two binary search tree built recursively before.
Note
Code
[Leetcode Solution]Balanced Binary Tree
Analysis
- Using BFS search each layer of binary tree from the root to leaf and find out the maximum depth and minimum depth
- If the difference between two depths is bigger then 1 then return false, else return true
Note
Code
[Leetcode Solution] Minimum Depth of Binary Tree
Analysis
- Using BFS search each layer of binary tree from the root to leaf.
- Check nodes in each layer. If some node has no child then that is the leaf in the minimum path to the root
- In practical using two vector alternatively to store the current and next layer of the tree when searching
Note
Code
[Leetcode Solution] Flatten Binary Tree to Linked List
Analysis
- A recursive out-of-place algorithm comes out
- A grace in-place algorithm can be found here
Note
- Define a return type as reference, then the return value could be used as a left-value
- In most case the recursion method may not be a in-place algorithm because the depth of recursion is usually in proportion to the input size. Thus the space used in store the stack is not constant
Code
Subscribe to:
Posts (Atom)