[Leetcode]由Binary Tree Right Side View说起

1. Binary Tree Right Side View

Binary Tree Right Side View应该是一道最近刚加上的新题。看着不难,我就试着做了做。

前两天看到这个题,就想到层次用DFS和层深度,找到每层最后一个节点。代码如下:

class Solution:
    # @param root, a tree node
    # @return a list of integers
    def rightSideView(self, root):
        nodeStack = []
        ret = []  #返回结果
        if root == None:
            return ret
        nodeStack.append(root)
        depth = [0]  #与nodeStack对应的节点深度list
        while nodeStack:  #dfs,空列表[]相当于False
            tmp = nodeStack.pop()
            d = depth.pop()
            if len(ret) <= d:  #如果ret中元素的个数不大于深度,说明该层的最后一个节点还没有加入
                ret.append(tmp.val)
            if tmp.left:
            #深度+1,左子节点入栈
                d += 1
                nodeStack.append(tmp.left)
                depth.append(d)
            if tmp.right:
            #深度+1(如果没有左子),右子节点入栈
                if not tmp.left: d += 1
                nodeStack.append(tmp.right)
                depth.append(d)

        return ret

当然,最好的方法还是直接用层次遍历。

class Solution:
    # @param root, a tree node
    # @return a list of integers
    def rightSideView(self, root):
        nodeQueue = []
        ret = []
        if root == None:
            return ret
        nodeQueue.append(root)
        while nodeQueue:
            qLen = len(nodeQueue) #刚进入while循环,qLen能得到当前层上的节点数
            for i in xrange(qLen):
                tmp = nodeQueue.pop(0)
                if i == qLen - 1:  #每次只记录层次遍历的最后一个节点
                    ret.append(tmp.val)
                if tmp.left:
                    nodeQueue.append(tmp.left)
                if tmp.right:
                    nodeQueue.append(tmp.right)
        return ret

思路和代码都不难理解。
那么一鼓作气,我们就顺便把其他几个简单的BFS/DFS题做了。


2.Maximum/Minimum Depth of Binary Tree

求深度,一个求最大,一个求最小。

最小深度

相对来说,我觉得最小深度更容易:BFS,一旦遇到一个叶节点,返回~
下面的程序中,我们直接把深度存到节点的val域里面去了(反正也用不上)。

class Solution:
    # @param root, a tree node
    # @return an integer
    def minDepth(self, root):
        if root == None: return 0
        root.val = 1
        a_queue = []
        a_queue.append(root)
        cur = None
        while a_queue:
            cur = a_queue.pop(0)
            if cur.left == None and cur.right == None:  #leaf node!
                return cur.val
            if cur.left:
                cur.left.val = cur.val + 1
                a_queue.append(cur.left)
            if cur.right:
                cur.right.val = cur.val + 1
                a_queue.append(cur.right)

最大深度

既可以用BFS做,也可以用DFS做。下面是BFS的代码。与minDepth差别是,这次我们要把所有节点都走一遍。队列中最后出队的节点,就是最深的那个。

class Solution:
    # @param root, a tree node
    # @return an integer
    def maxDepth(self, root):
        if root == None: return 0
        root.val = 1
        a_queue = []
        a_queue.append(root)
        cur_root = None
        while a_queue:
            cur_root = a_queue.pop(0)
            if cur_root.left:
                cur_root.left.val = cur_root.val + 1
                a_queue.append(cur_root.left)
            if cur_root.right:
                cur_root.right.val = cur_root.val + 1
                a_queue.append(cur_root.right)
        return cur_root.val

如果用DFS的话,直接递归,效率不高,但超简洁。

class Solution:
    # @param root, a tree node
    # @return an integer
    def maxDepth(self, root):
        if root == None:
            return 0
        return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1

Binary Tree Level Order Traversal/II/Zigzag Level Order Traversal

这几个就是简单粗暴的层次遍历了,只不过是变一下花样。也许各有各的特别的高效率做法,我还没有查。后续补充。

Binary Tree Level Order Traversal

基础版层次遍历很简单,前文的代码改一改就可以了。

class Solution:
    # @param root, a tree node
    # @return a list of lists of integers
    def levelOrder(self, root):
        nodeQueue = []
        ret = []
        if root == None:
            return ret
        nodeQueue.append(root)
        while nodeQueue:
            qLen = len(nodeQueue)
            thisLevel = []  #当前层的子list
            for i in xrange(qLen):
                tmp = nodeQueue.pop(0)
                thisLevel.append(tmp.val)
                if tmp.left:
                    nodeQueue.append(tmp.left)
                if tmp.right:
                    nodeQueue.append(tmp.right)
            ret.append(thisLevel)

        return ret  

效率稍差,可能还有优化的潜力。

Binary Tree Level Order Traversal II

II的差别就是从底向上层次遍历……我就直接把上面的程序改了一行……

        return ret[::-1]

我知道这样很不好,人家出题不可能这么无脑的。所以还会在看看。无论如何,结果是对的。

Binary Tree Zigzag Level Order Traversal

差别是,这次是蛇形的层次遍历:奇数行从左到右,偶数行从右到左。

class Solution:
    # @param root, a tree node
    # @return a list of lists of integers
    def zigzagLevelOrder(self, root):
        nodeQueue = []
        ret = []
        if root == None:
            return ret
        nodeQueue.append(root)
        level = 1
        while nodeQueue:
            qLen = len(nodeQueue)
            thisLevel = []
            for i in xrange(qLen):
                tmp = nodeQueue.pop(0)
                thisLevel.append(tmp.val)
                if tmp.left:
                    nodeQueue.append(tmp.left)
                if tmp.right:
                    nodeQueue.append(tmp.right)
            #新加的内容
            if level & 1:
                ret.append(thisLevel)
            else:
                ret.append(thisLevel[::-1])
            level += 1
            #...

        return ret 

目录

[TOC]来生成目录:

Markdown快捷键
- 加粗 Ctrl + B
- 斜体 Ctrl + I
- 引用 Ctrl + Q
- 插入链接 Ctrl + L
- 插入代码 Ctrl + K
- 插入图片 Ctrl + G
- 提升标题 Ctrl + H
- 有序列表 Ctrl + O
- 无序列表 Ctrl + U
- 横线 Ctrl + R
- 撤销 Ctrl + Z
- 重做 Ctrl + Y

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值