【leetcode?python】 257. Binary Tree Paths

深度优先搜索

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    # @param {TreeNode} root
    # @return {string[]}
    
    resultList=[]
    def binaryTreePaths(self, root):
        self.resultList=[]
        if root==None:
            return []
        else:
            self.resultList.append(str(root.val))
            self.dfs(root)
       
        return self.resultList
        
    def dfs(self,root):
      
        curVal=self.resultList[-1]
       
        if root.left!=None or root.right!=None:
            self.resultList.pop()
        else:return
         
        if root.left!=None:
            val=str(curVal)+‘->‘+str(root.left.val)
            self.resultList.append(val)
            self.dfs(root.left)
        if root.right!=None:
            val=str(curVal)+‘->‘+str(root.right.val)
            self.resultList.append(val)
            self.dfs(root.right)

时间: 2024-11-10 07:11:52

【leetcode?python】 257. Binary Tree Paths的相关文章

【LeetCode】257. Binary Tree Paths 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51474128 Subject 出处:https://leetcode.com/problems/binary-tree-paths/ Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-le

【LeetCode】257 - Binary Tree Paths

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"] Solution: /** * Definition for a binary tree node. * struct TreeNode {

【easy】257. Binary Tree Paths 二叉树找到所有路径

http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题--居然还是不会么-- /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ cl

<LeetCode OJ> 257. Binary Tree Paths

257. Binary Tree Paths My Submissions Question Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy 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: ["

leetCode 257. Binary Tree Paths 二叉树路径

257. Binary Tree Paths 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"] 思路: 1.采用二叉树的后序遍历非递归版 2.在叶子节点的时候处理字

【leetcode】 257. Binary Tree Path

/** * @author johnsondu * @time 2015.8.21 16:30 * @description tranverse a tree * @url https://leetcode.com/problems/binary-tree-paths/ */ /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; *

【leetcode?python】 111. Minimum Depth of Binary Tree

# Definition for a binary tree node.# class TreeNode(object):#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = None class Solution(object):    depthList=[]    def minDepth(self, root):        ""&q

【leetcode?python】 Maximum Depth of Binary Tree

#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = None class Solution(object):    def maxDepth(self, root):        if

LeetCode 257. Binary Tree Paths (二叉树路径)

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"] 题目标签:Tree 这道题目给了我们一个二叉树,让我们记录所有的路径,返回一个array string list. 我们可以另外设一个fin