[LeetCode]题解(python):102- Binary Tree Level Order Traversal

题目来源:

  https://leetcode.com/problems/binary-tree-level-order-traversal/



题意分析:

  宽度优先搜索一颗二叉树,其中同一层的放到同一个list里面。比如:

  3
   /   9  20
    /     15   7返回
[
  [3],
  [9,20],
  [15,7]
]


题目思路:

  新定义一个函数,加多一个层数参数,如果目前答案的列表答案个数等于层数,那么当前层数的列表append新元素,否者加新的层数。



代码(python):

  

# 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 levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        ans = []
        def bfs(root,level):
            if root != None:
                if len(ans) < level + 1:
                    ans.append([])
                ans[level].append(root.val)
                bfs(root.left,level+1)
                bfs(root.right,level+1)
        bfs(root,0)
        return ans
        

时间: 2024-10-12 21:49:57

[LeetCode]题解(python):102- Binary Tree Level Order Traversal的相关文章

LeetCode in Python 102. Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ]

刷题102. Binary Tree Level Order Traversal

一.题目说明 题目102. Binary Tree Level Order Traversal,给一个二叉树,返回按层遍历的各节点,每层返回一个数组. 二.我的解答 这个题目是普通的二叉树按层遍历,没什么难度.代码如下: class Solution{ public: vector<vector<int>> levelOrder(TreeNode* root){ vector<vector<int>> r; TreeNode* p; if(root ==NU

LeetCode 102 Binary Tree Level Order Traversal(二叉树的层级顺序遍历)(*)

翻译 给定一个二叉树,返回按层级顺序遍历的每个节点的值. 从左到右,逐层遍历. 例如: 给定一个二叉树 {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 返回它的层级顺序遍历结果为: [ [3], [9,20], [15,7] ] 翻译 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For

leetcode—102 Binary Tree Level Order Traversal(二叉树的层次遍历)

Binary Tree Level Order Traversal Total Accepted: 51429 Total Submissions: 174478 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7},

LeetCode之“树”:Binary Tree Level Order Traversal &amp;&amp; Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal 题目链接 题目要求: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level orde

Leetcode 102. Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] confu

Java [Leetcode 102]Binary Tree Level Order Traversal

题目描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ]

【LeetCode】102. Binary Tree Level Order Traversal

题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 提示

[LeetCode] 102. Binary Tree Level Order Traversal Java

题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7

Java for LeetCode 102 Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 解题思路: