题目链接:https://leetcode.com/problems/binary-tree-inorder-traversal/
解题思路:
二叉树的中序遍历。
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 class Solution { 11 public List<Integer> inorderTraversal(TreeNode root) { 12 13 Stack<TreeNode> stack = new Stack<>(); 14 15 List<Integer> res= new ArrayList<>(); 16 if(root==null) 17 return res; 18 19 TreeNode pnode = root; 20 while(pnode!=null || !stack.isEmpty()) 21 { 22 if(pnode!=null) 23 { 24 stack.push(pnode); 25 pnode = pnode.left; 26 } 27 else 28 { 29 pnode = stack.pop(); 30 res.add(pnode.val); 31 pnode = pnode.right; 32 } 33 } 34 return res; 35 } 36 }