美文网首页
【剑指Offer学习】【面试题6 :重建二叉树】

【剑指Offer学习】【面试题6 :重建二叉树】

作者: 果哥爸 | 来源:发表于2018-01-24 15:33 被阅读17次

    题目:

    输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如:前序遍历序列{ 1, 2, 4, 7, 3, 5, 6, 8}和中序遍历序列{4, 7, 2, 1, 5, 3, 8,6},重建出下图所示的二叉树并输出它的头结点。

    image.png

    解答:

    树节点:

    #import <Foundation/Foundation.h>
    // 树节点
    @interface NSBinaryTreeNode : NSObject
    // 值
    @property (nonatomic, strong) NSString *value;
    // 左节点
    @property (nonatomic, strong) NSBinaryTreeNode *left;
    // 右节点
    @property (nonatomic, strong) NSBinaryTreeNode *right;
    @end
    

    二叉树生成:

    #import "NSListNode.h"
    #import "NSBinaryTreeNode.h"
    #import <Foundation/Foundation.h>
    
    // 排序 二叉树
    NSBinaryTreeNode *sortOurBinaryTree(NSArray *preOrderArray, NSInteger preStartIndex, NSInteger preEndIndex,
                                        NSArray *inOrderArray,  NSInteger inStartIndex,   NSInteger inEndIndex) {
        
        if (preStartIndex > preEndIndex) {
            return nil;
        }
        NSBinaryTreeNode *binaryTreeNode = [[NSBinaryTreeNode alloc] init];
        binaryTreeNode.value = preOrderArray[preStartIndex];
        
        NSInteger tmpIndex = inStartIndex;
        while (tmpIndex <= inEndIndex && ([inOrderArray[tmpIndex] isEqualToString:binaryTreeNode.value] == NO)) {
            tmpIndex++;
        }
        
        // 如果在整个中序遍历的数组中没有找到,说明输入的参数是不合法的,抛出异常
        if (tmpIndex > inEndIndex) {
            NSLog(@"Invalid input");
            return nil;
        }
        
        // 递归构建当前根结点的左子树,左子树的元素个数:tmpIndex-inStartIndex+1个
        // 左子树对应的前序遍历的位置在[preStartIndex+1, preStartIndex+tmpIndex-inStartIndex]
        // 左子树对应的中序遍历的位置在[inStartIndex, tmpIndex-1]
        
        binaryTreeNode.left = sortOurBinaryTree(preOrderArray, preStartIndex + 1, preStartIndex + tmpIndex - inStartIndex, inOrderArray, inStartIndex, tmpIndex - 1);
        
        // 递归构建当前根结点的右子树,右子树的元素个数:inEndIndex-tmpIndex个
        // 右子树对应的前序遍历的位置在[preStartIndex+tmpIndex-inStartIndex+1, preStartIndex]
        // 右子树对应的中序遍历的位置在[tmpIndex+1, inEndIndex]
        binaryTreeNode.right = sortOurBinaryTree(preOrderArray, preStartIndex + tmpIndex - inStartIndex + 1, preEndIndex, inOrderArray, tmpIndex + 1, inEndIndex);
        return binaryTreeNode;
        
    }
    
    // 构建 二叉树
    NSBinaryTreeNode * constructBinaryTree(NSArray *preOrderArray, NSArray *inOrderArray) {
        if (preOrderArray == nil
            || inOrderArray == nil
            || preOrderArray.count == 0
            || inOrderArray.count == 0
            || preOrderArray.count != inOrderArray.count) {
            return nil;
        }
        
        
        return sortOurBinaryTree(preOrderArray, 0, preOrderArray.count - 1, inOrderArray, 0, inOrderArray.count - 1);
    }
    
    
    // 打印 二叉树
    void printBinaryTree(NSBinaryTreeNode *root) {
        
        if (root != nil) {
            
            NSLog(@"%@", root.value);
            printBinaryTree(root.left);
            printBinaryTree(root.right);
        }
    }
    
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            
            NSArray *preOrderArray = @[@"1", @"2", @"4", @"7", @"3", @"5", @"6", @"8"];
            NSArray *inOrderArray = @[@"4", @"7", @"2", @"1", @"5", @"3", @"8", @"6"];
            NSBinaryTreeNode *root = constructBinaryTree(preOrderArray, inOrderArray);
            printBinaryTree(root);
            
        }
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:【剑指Offer学习】【面试题6 :重建二叉树】

          本文链接:https://www.haomeiwen.com/subject/hxyuaxtx.html