dev-resources.site
for different kinds of informations.
C++ - Basic Tree Traversal(Recursive vs Stack)
Published at
8/1/2023
Categories
binary
tree
traversal
stack
Author
dinhluanbmt
Author
11 person written this
dinhluanbmt
open
Definition for a binary tree node
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 *left, TreeNode *right) : val(x), left(left), right(right) {}
};
Main types of binary tree traversal:
- Inorder (Traversal): left - root - right
- Preorder(Traversal): root - left - right
- Postorder(Traversal): left - right - root
leetcode question :
94. Binary Tree Inorder Traversal
Given the root of a binary tree, return the inorder traversal of its nodes' values.
- Recursive Solution
class Recursive_Solution {
public:
void travel(vector<int>& tree, TreeNode* root) {
if (!root) return;
travel(tree, root->left);
tree.push_back(root->val);
travel(tree, root->right);
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> ret;
travel(ret, root);
return ret;
}
};
- Non Recursive (using stack)
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
stack<TreeNode*> mst;
vector<int> ret;
TreeNode* tn = root;
while (tn || !mst.empty()) {
while (tn) {
mst.push(tn);
tn = tn->left;
}
tn = mst.top();
ret.push_back(tn->val);
mst.pop();
tn = tn->right;
}
return ret;
}
};
Leetcode question:
98. Validate Binary Search Tree
Given the root of a binary tree, determine if it is a valid binary search tree (BST).
A valid BST is defined as follows:
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 greater than the node's key.
Both the left and right subtrees must also be binary search trees.
Non Recursive (using Stack)
class Solution_stack {
public:
bool isValidBST(TreeNode* root) {
stack<TreeNode*> st;
TreeNode* tn = root;
TreeNode* prv = nullptr;
while(tn || !st.empty()){
while(tn){
st.push(tn);
tn = tn->left;
}
tn = st.top();
st.pop();
if(prv){
if(prv->val >= tn->val) return false;
}
prv = tn;
tn = tn->right;
}
return true;
}
};
Recursive solution
class Solution_recursive {
public:
bool g_ret = true;
TreeNode* prv = nullptr;
void check(TreeNode* root){
if(!root) return;
if(root->left) check(root->left);
if(prv){
if(prv->val >= root->val) {g_ret = false; return;}
}
prv = root;
check(root->right);
}
bool isValidBST(TreeNode* root) {
check(root);
return g_ret;
}
};
tree Article's
30 articles in total
House robber III
read article
Inorder traversal of a binary tree
read article
Comprehensive Tree Care Solutions in Gig Harbor and Tacoma with Pablo Tree Services
read article
Tree data structures in Rust with tree-ds (#2: Tree Operations)
read article
The Benefits of Hiring Professional Tree Trimmers in Christchurch
read article
Tree data structures in Rust with tree-ds (#1: Getting Started)
read article
DFS Traversal Guide: Easy way to remember DFS Traversel Path
read article
Chain - a Goofy, Functional, Tree-backed List
read article
Demystifying Tree Lopping vs. Tree Chipping: Which is Right for Your Landscape?
read article
Ergonomic Trees in Go
read article
Generating Dynamic Breadcrumb Menus Using Tree Table & Recursive CTE
read article
Types of decision tree in machine learning
read article
Tree Service Tips: Keeping Your Property Safe in Tinley Park
read article
What is tree data structure? 🌳
read article
Golang multinode tree with parallel search
read article
Implementing Nested Filters using React and Tree Data Structure
read article
814. Binary Tree Pruning
read article
A hierarchical tree component for React in Typescript
read article
C++ - Basic Tree Traversal(Recursive vs Queue)
read article
C++ - Basic Tree Traversal(Recursive vs Stack)
currently reading
Generate files and folder structures of your code
read article
Converting materialized paths into a tree with generics: a Golang kata
read article
wishing3 - if you'd 3 wishes, what'd they be?
read article
Represent a directory tree in a Github README.md
read article
Segment Trees - Part I
read article
A Nibble of Quadtrees in Rust
read article
What is AST?
read article
Finding all children of a node in a tree
read article
5 Reasons You Need Tree Doctor to Keep Your Plants Healthy
read article
Binary Tree Pruning
read article
Featured ones: