首页 > 其他分享 >LeetCode 257. 二叉树的所有路径

LeetCode 257. 二叉树的所有路径

时间:2023-05-16 16:57:52浏览次数:50  
标签:node paths 257 二叉树 LeetCode path root stack append

题目链接:LeetCode 257. 二叉树的所有路径

题意:

给你一个二叉树的根节点 root ,按 任意顺序 ,返回所有从根节点到叶子节点的路径。

解题思路:

递归法

采用递归法,就是一个dfs的过程,在遍历过程中,记录上路径即可。

完整代码如下:

var res []string  
var path []int
func binaryTreePaths(root *TreeNode) []string {
    res = []string{}
    path = []int{}
    dfs(root)
    return res
}
func dfs(root *TreeNode){
    path = append(path,root.Val)//将当前节点加入到路径中
    if root.Left == nil && root.Right == nil{//如果当前节点是叶子节点,则将当前的路径加入到结果集中
        line := strconv.Itoa(path[0])
        for i:=1;i<len(path);i++{
            line += "->" + strconv.Itoa(path[i])//每个都转换成string类型
        }
        res = append(res,line) //放入结果集
       
    }else{//如果当前节点不是叶子节点,则递归进行即可
        if root.Left != nil{
            dfs(root.Left)
        }
        if root.Right != nil{
             dfs(root.Right)
        }
    }
    path = path[:len(path)-1]  //恢复现场
}

迭代法

本题的迭代法相对较难理解,直接放代码。

迭代法代码:

func binaryTreePaths(root *TreeNode) []string {
	stack := []*TreeNode{}
	paths := make([]string, 0)
	res := make([]string, 0)
	if root != nil {
		stack = append(stack, root)
		paths = append(paths, "")
	}
	for len(stack) > 0 {
		l := len(stack)
		node := stack[l-1]
		path := paths[l-1]
		stack = stack[:l-1]
		paths = paths[:l-1]
		if node.Left == nil && node.Right == nil {
			res = append(res, path+strconv.Itoa(node.Val))
			continue
		}
		if node.Right != nil {
			stack = append(stack, node.Right)
			paths = append(paths, path+strconv.Itoa(node.Val)+"->")
		}
		if node.Left != nil {
			stack = append(stack, node.Left)
			paths = append(paths, path+strconv.Itoa(node.Val)+"->")
		}
	}
	return res
}

标签:node,paths,257,二叉树,LeetCode,path,root,stack,append
From: https://www.cnblogs.com/lxing-go/p/17406133.html

相关文章

  • LeetCode 111. 二叉树的最小深度
    题目链接:LeetCode111.二叉树的最小深度题意:给定一个二叉树,找出其最小深度。给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量。解题思路:1.递归法与求最大深度类似,采用先序或者后序都是可以的,但是这里要注意一个问题:最小深度是从......
  • [LeetCode] 1343. Number of Sub-arrays of Size K and Average Greater than or Equa
    Givenanarrayofintegers arr andtwointegers k and threshold,return *thenumberofsub-arraysofsize k andaveragegreaterthanorequalto *threshold.Example1:Input:arr=[2,2,2,2,5,5,5,8],k=3,threshold=4Output:3Explanation:Sub-a......
  • 图解LeetCode——234. 回文链表
    一、题目给你一个单链表的头节点head,请你判断该链表是否为回文链表。如果是,返回true;否则,返回false。二、示例2.1>示例1:【输入】head=[1,2,2,1]【输出】true2.2>示例2:【输入】head=[1,2]【输出】false提示:链表中节点数目在范围[1,10^5]内0<=Node.v......
  • 力扣---1448. 统计二叉树中好节点的数目
    给你一棵根为 root 的二叉树,请你返回二叉树中好节点的数目。「好节点」X定义为:从根到该节点X所经过的节点中,没有任何节点的值大于X的值。 示例1:输入:root=[3,1,4,3,null,1,5]输出:4解释:图中蓝色节点为好节点。根节点(3)永远是个好节点。节点4->(3,4)是路径中......
  • 力扣---104. 二叉树的最大深度
    给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。说明: 叶子节点是指没有子节点的节点。示例:给定二叉树[3,9,20,null,null,15,7],3/\920/\157返回它的最大深度 3。来源:力扣(LeetCode)链接:https://leetcode......
  • LeetCode 101. 对称二叉树
    题目链接:LeetCode101.对称二叉树题意:给你一个二叉树的根节点root,检查它是否轴对称。解题思路:1.递归法采用递归法思路就比较简单,因为要比较二叉树是否是轴对称的,因此就是比较左右子树是否轴对称,因此在遍历的过程中,就是比较左边的左子树与右边的右子树是否相等,以及左......
  • LeetCode 周赛 345(2023/05/14)体验一题多解的算法之美
    本文已收录到AndroidFamily,技术和职场问题,请关注公众号[彭旭锐]提问。往期回顾:LeetCode双周赛第104场·流水的动态规划,铁打的结构化思考周赛概览T1.找出转圈游戏输家(Easy)标签:模拟、计数T2.相邻值的按位异或(Medium)标签:模拟、数学、构造T3.矩阵中移动的最......
  • LeetCode 226. 翻转二叉树
    题目链接:LeetCode226.翻转二叉树题意:给你一棵二叉树的根节点root,翻转这棵二叉树,并返回其根节点。解题思路:对于每一个节点,只需要考虑反转当前节点的左右子树即可,因此只需要考虑遍历顺序,本题中,采用前序和后序遍历都是可以的,但是中序遍历不行,如果采用中序,会将某些节点反转两......
  • LeetCode 94. 二叉树的中序遍历
    题目链接:LeetCode94.二叉树的中序遍历题意:给定一个二叉树的根节点root,返回它的中序遍历。解题思路:对于二叉树的遍历,有递归和非递归两种遍历方式,1.递归遍历**根据“左->根->右”的顺序,直接模拟即可。注意按照递归三部曲(递归的参数和返回值、递归的终止条件、单层......
  • 二刷Leetcode-Days02
    栈和队列:/***232.用栈实现队列*队列的先进先出可以使用两个栈stackIn和stackOut来实现;*出队列前,如果stackOut不为空,表示队列当前值在上一轮已进入stackOut中,还没有被消费掉*若stackOut为空,也就是队列当前值还在stackIn中,为了确保先进先......