zoukankan      html  css  js  c++  java
  • 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
     
  • 相关阅读:
    Beetl 3中文文档 转载 http://ibeetl.com/guide/
    Beetl模板引擎入门教程
    Spring+Stomp+ActiveMq实现websocket长连接
    5672端口引发的一个大坑
    GeoServer中WMS、WFS的请求规范
    常用网址
    JAVA方法参数传递
    针对开发的缺陷管理
    不同逻辑顺序产生相同的结果编码如何决策
    怎样做一个软件项目经理
  • 原文地址:https://www.cnblogs.com/immiao0319/p/12955559.html
Copyright © 2011-2022 走看看