zoukankan      html  css  js  c++  java
  • leetcode--257. Binary Tree Paths

    1、问题描述

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

    For example, given the following binary tree:

       1
     /   
    2     3
     
      5

    All root-to-leaf paths are:

    ["1->2->5", "1->3"]

    2、边界条件:root为null;

    3、思路:递归,当前一个节点放入path中,左右子树分别继续往前走。左右子树只有当!= null时才继续找。

    base case:左右子节点都为空;root节点 == null 两种情况。

    不能用当前节点是否为空做base case,因为对于一个非空节点,如果有一个子节点,那么只有这个节点才是一条路径上的节点。

    4、代码实现

    方法一:

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public List<String> binaryTreePaths(TreeNode root) {
            List<String> results = new ArrayList<>();
            binaryTreePath(results, new ArrayList<Integer>(), root);
            return results;
        }
    
        public void binaryTreePath(List<String> results, List<Integer> path, TreeNode cur) {
            if (cur == null) {
                return; //root==null
            }
            if (cur.left == null && cur.right == null) {
                path.add(cur.val); //add cur's val
                String result = new String();
                result += path.get(0); //format
                for (int i = 1; i < path.size(); i++) {
                    result += "->" + path.get(i);
                }
                results.add(result);
                path.remove(path.size() - 1); //recover
                return;
            }
    
            if (cur.left != null) {
                path.add(cur.val);
                binaryTreePath(results, path, cur.left);
                path.remove(path.size() - 1);
            }
            if (cur.right != null) {
                path.add(cur.val);
                binaryTreePath(results, path, cur.right);
                path.remove(path.size() - 1);
            }
        }
    }

    优化

    class Solution {
        public List<String> binaryTreePaths(TreeNode root) {
            List<String> results = new ArrayList<>();
            binaryTreePath(results, new ArrayList<Integer>(), root);
            return results;
        }
    
        public void binaryTreePath(List<String> results, List<Integer> path, TreeNode cur) {
            if (cur == null) {
                return; //root==null
            }
            if (cur.left == null && cur.right == null) {//需要在上层判断
                path.add(cur.val); //add cur's val
                String result = new String();
                result += path.get(0); //format
                for (int i = 1; i < path.size(); i++) {
                    result += "->" + path.get(i);
                }
                results.add(result);
                path.remove(path.size() - 1); //recover
                return;
            }
            
            path.add(cur.val);
            if (cur.left != null) {
                binaryTreePath(results, path, cur.left);
            }
            if (cur.right != null) {
                binaryTreePath(results, path, cur.right);
            }
            path.remove(path.size() - 1);
        }
    }

     方法二

    class Solution {
        public List<String> binaryTreePaths(TreeNode root) {
            List<String> results = new ArrayList<>();
            if (root == null) {
                return results;
            }
            binaryTreePath(results, "", root);
            return results;
        }
    
        public void binaryTreePath(List<String> results, String path, TreeNode cur) {//直接操作字符串
            if (cur.left == null && cur.right == null) {
                results.add(path + cur.val);
                return;
            }
    
            if (cur.left != null) {
                binaryTreePath(results, path + cur.val + "->", cur.left);
            }
            if (cur.right != null) {
                binaryTreePath(results, path + cur.val + "->", cur.right);
            }
        }
    }

    5、api:

    result += path.get(0); ArrayList<Integer> path.
  • 相关阅读:
    redis.conf 配置信息:读取及修改命令
    Redis 持久化
    webpack 中,module、chunk、bundle 的区别(待补充)
    对象属性的描述:writable、enumerable、configurable
    webpack 中,importloaders 配置项的含义
    vue cli 3 中,Lint on save 与 Lint and fix on commit 区别(待补充)
    使用 vue-cli-service inspect 来查看一个 Vue CLI 3 项目的 webpack 配置信息(包括:development、production)
    Eslint 能自动格式化代码,为什么还要用 Prettier?
    prettier-eslint 与 prettier-eslint-cli 区别
    032_nginx配置文件安全下载
  • 原文地址:https://www.cnblogs.com/shihuvini/p/7455721.html
Copyright © 2011-2022 走看看