首页 > 其他 > 详细

226. Invert Binary Tree

时间:2020-05-26 13:37:43      阅读:45      评论:0      收藏:0      [点我收藏+]
package LeetCode_226

import java.util.*

/**
 * 226. Invert Binary Tree
 * https://leetcode.com/problems/invert-binary-tree/description/
 *
 * Invert a binary tree.
 *
 * Trivia:
This problem was inspired by this original tweet by Max Howell:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off.
 * */

class TreeNode(var `val`: Int) {
    var left: TreeNode? = null
    var right: TreeNode? = null
}

class Solution {
    fun invertTree(root: TreeNode?): TreeNode? {
        //method 1: bfs
        if (root==null){
            return root
        }
        val queue = LinkedList<TreeNode>()
        queue.offer(root)
        while (queue.isNotEmpty()){
            //invert level by level
            val cur = queue.pop()
            if (cur!=null){
                val temp = cur.left
                cur.left = cur.right
                cur.right = temp
            }
            if (cur.left!=null){
                queue.offer(cur.left)
            }
            if (cur.right!=null){
                queue.offer(cur.right)
            }
        }
        return root
    }
}

 

226. Invert Binary Tree

原文:https://www.cnblogs.com/johnnyzhao/p/12964800.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!