問題
在二元樹的中序遍歷中,我們訪問左節點,然後訪問根節點,最後訪問右節點。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<>(); inorder(root,list); return list; } public void inorder(TreeNode node, List<Integer> list){ if(node ==null) return; inorder(node.left,list); list.add(node.val); inorder(node.right,list); } }
以上是二元樹的中序遍歷的詳細內容。更多資訊請關注PHP中文網其他相關文章!