单页网站订单系统怎么改邮箱,企业微信开放平台,上海网站建设褐公洲司,wordpress4.9主题安装题目来源
力扣94二叉树的中序遍历
题目概述
给定一个二叉树的根节点 root #xff0c;返回 它的 中序 遍历 。
思路分析
就是简单的树的中序遍历#xff0c;使用递归和迭代的方式都可以实现。
代码实现
java实现
java使用迭代方式实现
public class Solution {publi…题目来源
力扣94二叉树的中序遍历
题目概述
给定一个二叉树的根节点 root 返回 它的 中序 遍历 。
思路分析
就是简单的树的中序遍历使用递归和迭代的方式都可以实现。
代码实现
java实现
java使用迭代方式实现
public class Solution {public ListInteger inorderTraversal(TreeNode root) {ListInteger res new ArrayList();StackTreeNode stack new Stack();while (root ! null || !stack.isEmpty()) {// 左孩子入栈等待方法while (root ! null) {stack.push(root);root root.left;}// 出栈被访问root stack.pop();res.add(root.val);// 如果有右孩子再访问右孩子root root.right;}return res;}
}
c实现
c使用递归方式实现
class Solution {
public:vectorint res vectorint();vectorint inorderTraversal(TreeNode* root) {if (root nullptr) {return res;}inorderTraversal(root-left);res.push_back(root-val);inorderTraversal(root-right);return res;}
}