Tree
1.Tree Definition
//Tree Declaration
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode():val(0),left(nullptr),right(nullptr){};
TreeNode(int x):val(x),left(nullptr),right(nullptr){};
TreeNode(int x, TreeNode* l,TreeNode* r):val(x),left(l),right(r){};
};
//Tree Initializaiton with default value zero
TreeNode *root = new TreeNode(0, nullptr, nullptr);
2.Binary Search Tree
2.1 Binary Search Tree Definition
Binary Search Tree is a node-based binary tree data structure which has following structures:
- The left subtree of a node contains only nodes with keys less than the node’s key.
- The right subtree of a node contains only nodes with keys larger than the node’s key.
- The left and right subtree each must also be a binary search tree.
BST Traversal
Inorder Traversal
BFS Solution:
BSTIterator(TreeNode* root) {
//create queue to store all node value in inorder sequence
queue<int> q;
if(!root)return;
stack<TreeNode*> s;
s.push(root);
TreeNode *node=s.top()->left;
while(!s.empty()||node){
while(node){
s.push(node);
node=node->left;
}
q.push(s.top()->val);
if(s.top()->right){
node=s.top->right;
}
s.pop();
}
}
- 173 binary search tree iterator(BFS)(stack/queue)
- 230 Kth Smallest Element in a BST(BFS/DFS)(Q:A)
- 98 Validate Binary Search Tree(Q:A)(BFS inorder traversal, DFS by setting lower and upper limit)
- 333 Largest BST Subtree(Q:A)(DFS by setting lower and upper limit)
Try to understand the difference how to set lower/upper limit between issue 98 and issue 333
Note that when comparing integer type value with long type value, an object of type int is always converted to the type long because type long has higher rank than type int.
- 337 House Robber III(Q:A) Note that intuitive approach would be to use boolean variable to check if current node is robbed or not, but it will create redundant calculations, improvement would be to return robbed value and not robbed value at the same time.
Note that if dfs traversal needs to compare output of everynodes while everynodes need to traverse to get its output value, then set output value of every tree node as return value, temporarily compared result as input and pass by reference.
Preorder Traversal(root->left->right)
-
199 Binary Tree Right Side View(Q:A)(BFS)(DFS->preorder traversal)
-
314 Binary Tree Vertical Order Traversal(Q:A)
Think about data structure to store nodes value.
Postorder Traversal(left->right->root)
Validate BST
Non-BST Traversal
Note that when reversely assigning or returning depth of nested list, using hash table as global variable to store value then loop through.
Binary Tree Basic Operation
Leaves
Sum
Height
Nodes
BST Ancestor
- 236 Lowest Common Ancestor of a Binary Tree(Q:A)
- 235 Lowest Common Ancestor of a Binary Search Tree(Q:A)
Note that if task is to need to traverse all nodes of binary tree meanwhile see if any nodes could fulfill requirements of tasks, then adding a new private result value and return state of every nodes during DFS.
2.3 Binary Search Tree Construction:
Ust two pointers to track range of inorder of current node, one or two pointer/index to track root of postorder of current node, note that traverse post order from right to left.
Construct List to BST
List->Array->DFS using boundary->Tree
2.4 Path
2.5 Modify Tree Data Structure
2.5.1 Flatten Binary Tree(DFS):
Since every depth search we need tail of left subtree to be connected to top of right subtree, return tail of every flatten root is necessary.
Don’t forget to set root left tree to be nullptr.
2.5.2 Next Pointer
We need to build ‘next’ connection between four tree nodes from their two root tree nodes.
Given dicussion above, dfs function would be like:
void func(TreeNode* left, TreeNode *right){}
Note that better solution would be breadth first search since it’s not perfect binary tree.