/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> inorderResult = new ArrayList<Integer>();
if(root == null) return inorderResult;
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while(!stack.isEmpty()) {
TreeNode cur = stack.pop();
if(cur.right != null) stack.push(cur.right);
stack.push(cur);
if(cur.left != null) stack.push(cur.left);
if(cur.left != null) {
cur.left = null;
cur.right = null;
}else if(cur.left == null) {
inorderResult.add(stack.pop().val);
}
}
return inorderResult;
}
}
Binary Tree Inorder Traversal
最新推荐文章于 2021-08-27 00:22:25 发布