每天刷個演算法題20160521:二叉樹高度(遞迴與非遞迴)

小飛_Xiaofei發表於2016-05-25

版權所有。所有權利保留。

歡迎轉載,轉載時請註明出處:

http://blog.csdn.net/xiaofei_it/article/details/51502727


為了防止思維僵化,每天刷個演算法題。已經刷了幾天了,現在發點程式碼。

我已經建了一個開源專案,每天的題目都在裡面:

https://github.com/Xiaofei-it/Algorithms

絕大部分演算法都是我自己寫的,沒有參考網上通用程式碼。讀者可能會覺得有的程式碼晦澀難懂,因為那是我自己的理解。

最近幾天都是在寫一些原來的東西,大多數是非遞迴。以後準備刷點DP、貪心之類的題。

下面是二叉樹高度(遞迴與非遞迴):

/**
 *
 * Copyright 2016 Xiaofei
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package xiaofei.algorithm;

import java.util.Stack;

/**
 * Created by Xiaofei on 16/5/21.
 *
 * I am writing code in Starbucks alone.
 *
 * What a sad picture!!!
 *
 * I cannot remember when I drank coffee with somebody else last time.
 *
 */
public class BinaryTreeHeight {

    public static int calculateRecursively(BinaryTreeNode root) {
        if (root == null) {
            return 0;
        } else {
            return Math.max(calculateRecursively(root.left), calculateRecursively(root.right)) + 1;
        }
    }

    public static int calculateCorecursively(BinaryTreeNode root) {
        //當年ACM的時候總喜歡自己寫queue或者stack,幾乎不用C++的STL庫。現在想用一下Java的Stack
        class Element {
            BinaryTreeNode node;
            int left;
            int right;
            Element(BinaryTreeNode node) {
                this.node = node;
                left = right = -1;
            }
        }
        Stack<Element> stack = new Stack<>();
        stack.push(new Element(root));
        int result = -1;
        while (!stack.empty()) {
            Element element = stack.peek();
            if (element.node == null) {
                stack.pop();
                if (stack.isEmpty()) {
                    result = 0;
                } else {
                    Element tmp = stack.peek();
                    if (tmp.left == -1) {
                        tmp.left = 0;
                    } else {
                        tmp.right = 0;
                    }
                }
            } else if (element.left == -1) {
                stack.push(new Element(element.node.left));
            } else if (element.right == -1) {
                stack.push(new Element(element.node.right));
            } else {
                Element tmp1 = stack.pop();
                int height = Math.max(tmp1.left, tmp1.right) + 1;
                if (stack.isEmpty()) {
                    result = height;
                } else {
                    Element tmp2 = stack.peek();
                    if (tmp2.left == -1) {
                        tmp2.left = height;
                    } else {
                        tmp2.right = height;
                    }
                }
            }
        }
        return result;
    }
}


相關文章