• 257. Binary Tree Paths 257.二叉树路径


    Given a binary tree, return all root-to-leaf paths.

    Note: A leaf is a node with no children.

    Example:

    Input:
    
       1
     /   
    2     3
     
      5
    
    Output: ["1->2->5", "1->3"]
    
    Explanation: All root-to-leaf paths are: 1->2->5, 1->3

    这种路径题的也不一定要用root.right = ,路径内容:DFS(起点,过程,结果)


    就是分成有等号的DC、没有等号的recursive两种:

    root.left = trimBST(root.left, L, R);自己等于自己的调用

    convertBST(root.right); 就光是自己
    node.left = helper(nums, left, mid - 1); index

    helper() 就还是左右都加?这中间怎么权衡的啊。左边的计算就只包括左边一线,这是traverse的自带优势。以前不懂。

    叶子节点的特征是左右都没东西了,这个需要利用。从而找到一个字符串的开头。

    树立概念吧:退出情况是叶子节点(左右为空),path + root。一般情况是左右递归

    class Solution {
        public List<String> binaryTreePaths(TreeNode root) {
            List<String> ans = new ArrayList<String>();
            //cc
            if (root == null) return ans;
            findBT(root, "", ans);
            return ans;
        }
        
        public void findBT(TreeNode root, String path, List<String> ans) {
            //边界情况,叶子节点
            if ((root.left == null) && (root.right == null))
                ans.add(path + root.val);
            
            //左右递归
            if (root.left != null)
                findBT(root.left, path + root.val + "->", ans);
            if (root.right != null)
                findBT(root.right, path + root.val + "->", ans);
        }
    }
    View Code
     
  • 相关阅读:
    dubbo配置文件解读(1)
    Java同步与异步
    java垃圾回收
    Java中的String,StringBuilder,StringBuffer三者的区别
    ThreadLocal终极篇
    TCP/IP协议与HTTP协议(一)
    TCP/IP协议与HTTP协议(二)
    springcloud之断路器(Hystrix)
    解决mysql不能通过'/tmp/mysql.sock 连接的问题
    jQuery.extend 函数使用详解
  • 原文地址:https://www.cnblogs.com/immiao0319/p/12955559.html
Copyright © 2020-2023  润新知