leetcode

    xiaoxiao2021-03-26  24

    题意:

    给一个二叉树,返回其中序遍历。

    分析:

    我们递归深搜。确定递归函数的参数是当前搜索节点,结束条件是当前搜索节点为空。

    遍历的顺序是,搜左,放入,搜右。

    /**  * Definition for a binary tree node.  * public class TreeNode {  *     int val;  *     TreeNode left;  *     TreeNode right;  *     TreeNode(int x) { val = x; }  * }  */ public class Solution {     List<Integer> list = new ArrayList<>();     public List<Integer> inorderTraversal(TreeNode root) {         helper(root);         return list;     }          public void helper(TreeNode root){         if(root == null){             return;         }                 // if(root.left != null)             helper(root.left);         list.add(root.val);        // if(root.right != null)             helper(root.right);     } }

    转载请注明原文地址: https://ju.6miu.com/read-659121.html

    最新回复(0)