tree related problems (update continuously)
来源:互联网 发布:淘宝网上兼职可靠吗 编辑:程序博客网 时间:2024/05/18 00:53
leetcode Binary Tree Level Order Traversal
这道题是要进行二叉树的层次遍历,对于层次遍历,最简单直观的办法就是进行BFS。于是我们只需要维护一个队列就可以了,队列里面的元素需要记录该节点的内容和节点所在的层,依次从队列中取出节点进行扩展就可以了。
# Definition for a binary tree node# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: # @param root, a tree node # @return a list of lists of integers def levelOrder(self, root): if(root == None): return [] queue = [] queue.append((0, root)) ans = [] cur = 0 cur_nodes = [] while len(queue) != 0: level, fa = queue.pop(0) if(level == cur): cur_nodes.append(fa.val) else: ans.append(cur_nodes) cur_nodes = [] cur_nodes.append(fa.val) cur = level if fa.left != None: queue.append((level+1, fa.left)) if fa.right != None: queue.append((level+1, fa.right)) ans.append(cur_nodes) return ans
0 0
- tree related problems (update continuously)
- dp related problems (update continuously)
- Related Problems
- C++ 编程技巧积累 (Update Continuously)
- Top 10 NameNode-related problems
- Some tricky problems related to "protected"
- Binary tree related algorithms summary
- Continuously Integrate
- Algorithm: Binary Tree(1) -- Traversal Problems
- The solution to search related problems on Geonetwork: operators, quotes, phrase, chinese.
- when using pod update yajl-objc , occurs some problems.
- 水题大作战-updated continuously
- ARM terms, updating continuously.
- Problems
- Problems
- Problems
- The problems were related to the component side and also to the client that uses the component
- How to Analyze Problems Related to Internal Errors (ORA-600) and Core Dumps (ORA-7445) using My Orac
- Leetcode:populating_next_right_pointers_in_each_node题解
- 关于Alipay支付宝接口(Java版)
- 2018 母牛的故事
- Java设计模式菜鸟系列(八)适配器模式建模与实现
- 大数据量的算法面试题
- tree related problems (update continuously)
- 【无限互联】IOS框架研究之SDWebImage的原理以及使用流程
- 算式表达式(不转化逆波兰)
- C++随笔(1)
- File Input/Output in Python
- 重修C++的大三老人笔记
- 提高Win下的操作效率系列之一-----老曹的wintalkshow
- poj 1741 Tree 树分治
- mac 常用软件