标签:pre nod div nta nbsp root title turn 迭代
给定一个二叉树,返回它的中序 遍历。
示例:
输入: [1,null,2,3] 1 2 / 3 输出: [1,3,2]
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public List<Integer> inorderTraversal(TreeNode root) { //迭代 - Stack<TreeNode> sta = new Stack<>(); List<Integer> list = new ArrayList<>(); while(root != null || !sta.isEmpty()){ while(root != null){ sta.push(root); root = root.left; } TreeNode node = sta.pop(); list.add(node.val); //换为右节点 root = node.right; } return list; } }
标签:pre nod div nta nbsp root title turn 迭代
原文地址:https://www.cnblogs.com/zzytxl/p/12682711.html