递归遍历
文章讲解:https://programmercarl.com/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E9%80%92%E5%BD%92%E9%81%8D%E5%8E%86.html#%E6%80%9D%E8%B7%AF
视频讲解:https://www.bilibili.com/video/BV1Wh411S7xt/?spm_id_from=333.788&vd_source=e70917aa6392827d1ccc8d85e19e8375
题目链接:https://programmercarl.com/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E9%80%92%E5%BD%92%E9%81%8D%E5%8E%86.html#%E6%80%9D%E8%B7%AF
实现情况:
/*** 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) {}* };*/
class Solution {
public:void traversal(TreeNode *cur,vector<int>& vec){if(cur == NULL)return;vec.push_back(cur->val);//根traversal(cur->left,vec);//左traversal(cur->right,vec);//右}vector<int> preorderTraversal(TreeNode* root) {vector<int> result;traversal(root,result);return result;}
};
题目链接:https://leetcode.cn/problems/binary-tree-postorder-traversal/description/
实现情况:
/*** 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) {}* };*/
class Solution {
public:void traversal(TreeNode* cur, vector<int>& vec) {if (cur == NULL)return;traversal(cur->left, vec); // 左traversal(cur->right, vec); // 右vec.push_back(cur->val); // 根}vector<int> postorderTraversal(TreeNode* root) {vector<int> result;traversal(root, result);return result;}
};
题目链接:https://leetcode.cn/problems/binary-tree-inorder-traversal/description/
实现情况:
/*** 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) {}* };*/
class Solution {
public:void traversal(TreeNode* cur, vector<int>& vec) {if (cur == NULL)return;traversal(cur->left, vec); // 左vec.push_back(cur->val); // 根traversal(cur->right, vec); // 右}vector<int> inorderTraversal(TreeNode* root) {vector<int> result;traversal(root, result);return result;}
};
迭代遍历
题目链接:https://leetcode.cn/problems/binary-tree-preorder-traversal/
实现情况:
/*** 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) {}* };*/
class Solution {
public:vector<int> preorderTraversal(TreeNode* root) {stack<TreeNode*> st; //vector<int> result;if (root == NULL)return result;st.push(root);while (!st.empty()) {TreeNode* node = st.top(); // 根节点先出来st.pop();result.push_back(node->val);if (node->right)//先放右孩子st.push(node->right);if (node->left)st.push(node->left);}return result;}
};
题目链接:[:https://leetcode.cn/problems/binary-tree-postorder-traversal/description/]
实现情况:
/*** 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) {}* };*/
class Solution {
public:vector<int> inorderTraversal(TreeNode* root) {vector<int> result;stack<TreeNode*> st;TreeNode* cur = root;while (cur != NULL || !st.empty()) {if (cur != NULL) {st.push(cur);cur = cur->left;} else {cur = st.top();st.pop();result.push_back(cur->val);cur = cur->right;}}return result;}
};
题目链接:https://leetcode.cn/problems/binary-tree-postorder-traversal/
实现情况:
/*** 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) {}* };*/
class Solution {
public:vector<int> postorderTraversal(TreeNode* root) {stack<TreeNode*> st;vector<int> result;if (root == NULL)return result;st.push(root);while (!st.empty()) {TreeNode* node = st.top();st.pop();result.push_back(node->val); // 根节点最先进去if (node->left)st.push(node->left);if (node->right)st.push(node->right);}reverse(result.begin(), result.end()); // 翻转后输出return result;}
};
统一迭代
题目链接:https://leetcode.cn/problems/binary-tree-inorder-traversal/
实现情况:
/*** 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) {}* };*/
class Solution {
public:vector<int> inorderTraversal(TreeNode* root) {vector<int> result;stack<TreeNode*> st;if (root != NULL)st.push(root);while (!st.empty()) {TreeNode* node = st.top();st.pop();if (node != NULL) {if (node->right) {st.push(node->right);} // 添加右节点(空节点不入栈)st.push(node); // 添加中节点st.push(NULL); // 做标记if (node->left) {st.push(node->left);} // 添加左节点(空节点不入栈)} else { // 只有遇到空节点的时候,才将下一个节点放进结果集node = st.top(); // 重新取出栈中元素st.pop();result.push_back(node->val); // 加入到结果集}}return result;}
};
题目链接:
实现情况:
/*** 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) {}* };*/
class Solution {
public:vector<int> preorderTraversal(TreeNode* root) {vector<int> result;stack<TreeNode*> st;if (root != NULL)st.push(root);while (!st.empty()) {TreeNode* node = st.top();st.pop();if (node != NULL) {if (node->right) {st.push(node->right);} // 添加右节点(空节点不入栈)if (node->left) {st.push(node->left);} // 添加左节点(空节点不入栈)st.push(node); // 添加中节点st.push(NULL); // 做标记} else { // 只有遇到空节点的时候,才将下一个节点放进结果集node = st.top(); // 重新取出栈中元素st.pop();result.push_back(node->val); // 加入到结果集}}return result;}
};
题目链接:
实现情况:
/*** 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) {}* };*/
class Solution {
public:vector<int> postorderTraversal(TreeNode* root) {vector<int> result;stack<TreeNode*> st;if (root != NULL)st.push(root);while (!st.empty()) {TreeNode* node = st.top();st.pop();if (node != NULL) {st.push(node); // 添加中节点st.push(NULL); // 做标记if (node->right) {st.push(node->right);} // 添加右节点(空节点不入栈)if (node->left) {st.push(node->left);} // 添加左节点(空节点不入栈)} else { // 只有遇到空节点的时候,才将下一个节点放进结果集node = st.top(); // 重新取出栈中元素st.pop();result.push_back(node->val); // 加入到结果集}}return result;}
};
层序遍历
文章讲解:https://programmercarl.com/0102.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E5%B1%82%E5%BA%8F%E9%81%8D%E5%8E%86.html
视频讲解:https://www.bilibili.com/video/BV1GY4y1u7b2/?spm_id_from=333.788&vd_source=e70917aa6392827d1ccc8d85e19e8375
题目链接:
https://leetcode.cn/problems/maximum-depth-of-binary-tree/
给定一个二叉树 root ,返回其最大深度。
二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
实现情况:
深度:任意一个节点到根节点的距离;前序遍历
高度:根节点到叶子节点的距离;后序变量
这里使用层序遍历
/*** 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) {}* };*/
class Solution {
public:int maxDepth(TreeNode* root) {if(root == NULL)return 0;int depth = 0;queue<TreeNode*> que;que.push(root);while(!que.empty()){int size = que.size();depth++;//记录深度for(int s = 0; s <size; s++){TreeNode* node = que.front();//中que.pop();if(node->left){//左que.push(node->left);}if(node->right){//右que.push(node->right);}}}return depth;}
};
题目链接:
https://leetcode.cn/problems/minimum-depth-of-binary-tree/description/
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明:叶子节点是指没有子节点的节点。
实现情况:
/*** 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) {}* };*/
class Solution {
public:int minDepth(TreeNode* root) {if (root == NULL)return 0;int depth = 0;queue<TreeNode*> que;que.push(root);while (!que.empty()) {int size = que.size();depth++; // 记录最小深度for (int i = 0; i < size; i++) {TreeNode* node = que.front();que.pop();if (node->left)que.push(node->left);if (node->right)que.push(node->right);if (!node->left &&!node->right) { // 当左右孩子都为空的时候,说明是最低点的一层了,退出return depth;}}}return depth;}}
;