Binary Tree: Inorder Traversal
来源:互联网 发布:淘宝400电话 编辑:程序博客网 时间:2024/06/05 09:44
11/1
the classic inorder traversal, takes O(n) time and O(1) space
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public List<Integer> inorderTraversal(TreeNode root) { // The Morris Traversal List<Integer> ans = new ArrayList<Integer>(); TreeNode p = root; while(p != null){ if(p.left == null){ ans.add(p.val); p = p.right; }else{ // p.left is not null TreeNode rightM = p.left; while(rightM.right != null && rightM.right != p){ rightM = rightM.right; } if(rightM.right == null){ rightM.right = p; p = p.left; }else{ ans.add(p.val); rightM.right = null; p = p.right; } } } return ans; }}
0 0
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Objective-C (NSObject isKindOfClass、isMemberOfClass 、conformsToProtocol、isSubclassOfClass)
- USACO Electric Fences 解题报告
- First Missing Integer
- 交叉编译Cubietruck的u-boot
- dfgdfg
- Binary Tree: Inorder Traversal
- fdgdfg
- iOS 常见错误, 失误
- Max-Sum Subarray
- 解决CentOS 6.5更新后无法联网的问题以及利用yum的 yum-versionlock 插件锁定某个软件版本不升级的方法
- 黑马程序员—C语言笔记—了解Mac的基本使用和C语言基础语法
- 设计实例对比:MySQL vs MongoDB
- Longest Substring without Repeating Characters
- [Leetcode]Interleaving String