请一键三连, 非常感谢LeetCode 力扣题解94. 二叉树的中序遍历94. Binary Tree Inorder Traversal帮你深度理解 二叉树 数据结构 递归算法 Morris遍历 迭代, 视频播放量 116、弹幕量 0、点赞数 0、投硬币枚数 0、收藏人数 0、转发人数 0, 视频作者 程序员写代码, 作者简介 Lee
叫 Morris Traversal,在介绍这种方法之前,先来引入一种新型树,叫Threaded binary tree,这个还不太好翻译,第一眼看上去以为是叫线程二叉树,但是感觉好像又跟线程没啥关系,后来看到网上有人翻译为螺纹二叉树,但博主认为这翻译也不太敢直视,很容易让人联想到为计划生育做出突出贡献的某世界...
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };*/classSolution {public: vector<int> inorderTraversal(TreeNode*root) { vector<int>ans; TreeNode* temp =newTreeNode(0);//因为我们不断让root=root->right,所以判断条件为root != NULLwhile(root !=NULL) {//root有左子树,...
publicList<Integer>inorderTraversal(TreeNoderoot){List<Integer>ans=newArrayList<>();Stack<TreeNode>stack=newStack<>();TreeNodecur=root;while(cur!=null||!stack.isEmpty()){//节点不为空一直压栈while(cur!=null){stack.push(cur);cur=cur.left;//考虑左子树}//节点为空,就出栈cur=stack.pop()...
vector<int> inorderTraversal(TreeNode* root) { vector<int> res; inorder(res,root); return res; } private: void inorder(vector<int>& res,TreeNode* root){ if(root==NULL) return; inorder(res,root->left); res.push_back(root->val); ...
94. Binary Tree Inorder Traversal 难度:medium Given abinary tree, return theinordertraversal of its nodes' values. 知识点: Recursion Iteration Threaded binary tree 二刷: 要求的iteration: 最初做法: class Solution: def inorderTraversal(self, root: TreeNode) -> List[int]: ...
每天一算:Binary Tree Inorder Traversal 程序员吴师兄 + 关注 预计阅读时间2分钟 6 年前 LeetCode上第94 号问题:二叉树的中序遍历 题目 给定一个二叉树,返回它的 中序 遍历。 示例: 输入: [1,null,2,3] 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 用栈(Stack)的...
*/classSolution{public List<Integer>inorderTraversal(TreeNode root){List<Integer>result=newLinkedList<>();TreeNode current=root;TreeNode prev=null;while(current!=null){// left firstif(current.left==null){result.add(current.val);current=current.right;}// if there is left, get the rightmost...
* TreeNode right; * TreeNode(int x) { val = x; } * } */publicclassSolution{public List<Integer>inorderTraversal(TreeNode root){ArrayList<Integer>result=newArrayList<Integer>();if(root==null)returnresult;inorderTraversal(root,result);returnresult;}privatevoidinorderTraversal(TreeNode root,Arra...
Binary tree traversal: Preorder, Inorder, and Postorder In order to illustrate few of the binary tree traversals, let us consider the below binary tree: Preorder traversal: To traverse a binary tree in Preorder, following operations are carried-out (i) Visit the root, (ii) Traverse the le...