Given a binary tree, return the inorder traversal of its nodes' values.
Example:
Input: [1,null,2,3] 1 \ 2 / 3Output: [1,3,2]
Follow up: Recursive solution is trivial, could you do it iteratively?
难度:medium
题目:给定二叉树,返回其中序遍历结点。(不要使用递归)
思路:栈
Runtime: 1 ms, faster than 55.50% of Java online submissions for Binary Tree Inorder Traversal.
Memory Usage: 36.2 MB, less than 100.00% of Java online submissions for Binary Tree Inorder Traversal./** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public ListinorderTraversal(TreeNode root) { Stack stack = new Stack<>(); List result = new ArrayList<>(); while (!stack.isEmpty() || root != null) { if (root != null) { stack.push(root); root = root.left; } else { TreeNode node = stack.pop(); result.add(node.val); root = node.right; } } return result; }}