004-算法面试必备-二叉树的路径

打印二叉树的路径

这是leetcode 257题,这题竟然是easy题,看来革命尚未成功,我辈仍需努力呀

题目描述:给定一个二叉树,输出二叉树从根节点到叶子结点的所有路径

Input:

   1
 /   \
2     3
 \
  5

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

Explanation: All root-to-leaf paths are: 1->2->5, 1->3

方案设计:

我这里采用了两种方式进行的

1:利用递归,

     根值->{左孩子为首的字符串}

     根值->{右孩子为首的字符串}

      如果遍历到叶子节点,就将整个路径字符串put到数组中

2:利用了返回值

    先递归到叶子节点,然后插入叶子节点的值 

    根节点->利用左孩子返回的路径字符串

    根节点->利用右孩子返回的路径字符串

leetcode进行测试,第一种方案竟然打败了86%的java提交

下面看代码吧

import java.util.LinkedList;
class Solution_BinaryPath0703{
	class TreeNode{
		int val;
		TreeNode left;
		TreeNode right;
		TreeNode(int val){
			this.val = val;
		}
	}
	LinkedList<String> res;
	private void path(TreeNode root,String s){  //要在s后面的字符串中追加字符
		if(root.left == null&&root.right==null){
			String ss = s + "->" + Integer.toString(root.val);
			res.add(ss);
			return;
		}
		if(root.left != null){
			String ss = s + "->" + Integer.toString(root.val);
			path(root.left,ss);
		}
		if(root.right != null){
			String ss = s + "->" +Integer.toString(root.val);
			path(root.right,ss);
		}
		return;
	}
	public LinkedList<String> binaryPaths(TreeNode root){
		res = new LinkedList<String>();
		if(root == null)return res;
        if(root.left == null && root.right == null){
        	res.add(Integer.toString(root.val));
            return res;
        }
		if(root.left != null){
			path(root.left,Integer.toString(root.val));
		}
		if(root.right !=null){
			path(root.right,Integer.toString(root.val));
		}
		return res;	
	}
}
public class M05_BinaryPath {
	class TreeNode{
		int val;
		TreeNode left;
		TreeNode right;
		TreeNode(int val){
			this.val = val;
		}
	}
    public LinkedList<String> binaryTreePaths(TreeNode root) {
    	LinkedList<String> ls = new LinkedList<String>();
    	if(root == null) return ls;
        if(root.left==null && root.right == null){  //表示当前节点就是叶子节点
        	ls.add(Integer.toString(root.val));   //或者使用""+root.val
        	return ls;
        }
        if(root.left != null){
        	LinkedList<String> leftls = binaryTreePaths(root.left);
        	for(String ss:leftls){  //代表了历史的字
            	StringBuilder cur = new StringBuilder(Integer.toString(root.val));
        		cur.append("->");
        		cur.append(ss);
        		ls.add(cur.toString());
        	}
        }
        if(root.right != null){
        	LinkedList<String> rightls = binaryTreePaths(root.right);
        	for(String ss:rightls){  //代表了历史的字
        		StringBuilder cur = new StringBuilder(Integer.toString(root.val));
        		cur.append("->");
        		cur.append(ss);
        		ls.add(cur.toString());
        	}
        }
        return ls;
    }
	public static void main(String[] args){
		M05_BinaryPath ss = new M05_BinaryPath();
		M05_BinaryPath.TreeNode node0 = ss.new TreeNode(0);
		M05_BinaryPath.TreeNode node1 = ss.new TreeNode(1);
		M05_BinaryPath.TreeNode node2 = ss.new TreeNode(2);
		M05_BinaryPath.TreeNode node3 = ss.new TreeNode(3);
		M05_BinaryPath.TreeNode node4 = ss.new TreeNode(4);
		M05_BinaryPath.TreeNode node5 = ss.new TreeNode(5);
		node0.left = node1;
		node0.right = node2;
		node1.left = node3;
		node1.right = node4;
		node2.right = node5;
		LinkedList<String> ls = ss.binaryTreePaths(node0);
		for(String s:ls){
			System.out.println(s);
		}
		System.out.println("0703");
		Solution_BinaryPath0703 sss = new Solution_BinaryPath0703();
		Solution_BinaryPath0703.TreeNode node00 = sss.new TreeNode(0);
		Solution_BinaryPath0703.TreeNode node11 = sss.new TreeNode(1);
		Solution_BinaryPath0703.TreeNode node22 = sss.new TreeNode(2);
		Solution_BinaryPath0703.TreeNode node33 = sss.new TreeNode(3);
		Solution_BinaryPath0703.TreeNode node44 = sss.new TreeNode(4);
		Solution_BinaryPath0703.TreeNode node55 = sss.new TreeNode(5);
		node00.left = node11;
		node00.right = node22;
		node11.left = node33;
		node11.right = node44;
		node22.right = node55;
		LinkedList<String> ls2 = sss.binaryPaths(node00);
		for(String s:ls2){
			System.out.println(s);
		}
	}
}


 

 

 

 

    原文作者:weifengLD
    原文地址: https://blog.csdn.net/u013385925/article/details/80906042
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞