leetcode 二叉树的前序、中序、后序、层次遍历(OC)

144. 二叉树的前序遍历

地址:https://leetcode-cn.com/problems/binary-tree-preorder-traversal/

94.二叉树的中序遍历

地址:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/

145.二叉树的后序遍历

地址:https://leetcode-cn.com/problems/binary-tree-postorder-traversal/

102.二叉树的层序遍历

地址:https://leetcode-cn.com/problems/binary-tree-level-order-traversal/

给定一个二叉树的根节点 root ,返回它的 中序 遍历。

示例 1:
输入:root = [1,null,2,3]
输出:[1,3,2]

示例 2:
输入:root = []
输出:[]

示例 3:
输入:root = [1]
输出:[1]

示例 4:
输入:root = [1,2]
输出:[2,1]

示例 5:
输入:root = [1,null,2]
输出:[1,2]

提示:
树中节点数目在范围 [0, 100] 内
-100 <= Node.val <= 100

图片显示不出来,请直接查看力扣原地址

前序遍历 先遍历根,再访问左子树,再遍历右子树
中序遍历 先遍历左子树,再访问根,再遍历右子树
后序遍历 先遍历根,再访问右子树,再遍历左子树

使用OC 我们需要创建一个BinaryTreeNode节点model 里面的对象如下

/**
 *  值
 */
@property (nonatomic, assign) NSInteger value;
/**
 *  左节点
 */
@property (nonatomic, strong) BinaryTreeNode *leftNode;
/**
 *  右节点
 */
@property (nonatomic, strong) BinaryTreeNode *rightNode;

方法1:递归

/**
 *  前序遍历
 *  先遍历根,再访问左子树,再遍历右子树
 *
 *  @param rootNode 根节点
 */
- (NSArray *)preorderTraversal1:(BinaryTreeNode *)rootNode
{
    if (rootNode == nil) {
        return @[];
    }
    
    NSMutableArray *inorderArray = [NSMutableArray array];
    
    [inorderArray addObject:@(rootNode.value)];
    
    if (rootNode.leftNode) {
        NSArray *leftArray = [self preorderTraversal1:rootNode.leftNode];
        [inorderArray addObjectsFromArray:leftArray];
    }
    
    if (rootNode.rightNode) {
        NSArray *rightArray = [self preorderTraversal1:rootNode.rightNode];
        [inorderArray addObjectsFromArray:rightArray];
    }
    
    return inorderArray;
}

/**
 *  中序遍历
 *  先遍历左子树,再访问根,再遍历右子树
 *
 *  @param rootNode 根节点
 */
- (NSArray *)inorderTraversal1:(BinaryTreeNode *)rootNode
{
    if (rootNode == nil) {
        return @[];
    }
    
    NSMutableArray *inorderArray = [NSMutableArray array];
    
    if (rootNode.leftNode) {
        NSArray *leftArray = [self inorderTraversal1:rootNode.leftNode];
        [inorderArray addObjectsFromArray:leftArray];
    }
    
    [inorderArray addObject:@(rootNode.value)];
    
    if (rootNode.rightNode) {
        NSArray *rightArray = [self inorderTraversal1:rootNode.rightNode];
        [inorderArray addObjectsFromArray:rightArray];
    }
    
    return inorderArray;
}


/**
 *  后序遍历
 *  先访问左子树,再遍历右子树 最后根
 *
 *  @param rootNode 根节点
 */
- (NSArray *)postorderTraversal1:(BinaryTreeNode *)rootNode
{
    if (rootNode == nil) {
        return @[];
    }
    
    NSMutableArray *inorderArray = [NSMutableArray array];
    
    if (rootNode.leftNode) {
        NSArray *leftArray = [self postorderTraversal1:rootNode.leftNode];
        [inorderArray addObjectsFromArray:leftArray];
    }
    
    if (rootNode.rightNode) {
        NSArray *rightArray = [self postorderTraversal1:rootNode.rightNode];
        [inorderArray addObjectsFromArray:rightArray];
    }
    
    [inorderArray addObject:@(rootNode.value)];
    
    return inorderArray;
}

递归比较简单 没啥好说的

方法2:迭代

/// 前序遍历 迭代
/// @param rootNode  根节点
- (NSArray *)preorderTraversal2:(BinaryTreeNode *)rootNode
{
    if (rootNode == nil) {
        return @[];
    }
    
    NSMutableArray *inorderArray = [NSMutableArray array];
        
    NSMutableArray *numbers = [NSMutableArray array];
    
    while (rootNode != nil || inorderArray.count > 0) {
        
        ///将根节点的值取出来,放入数组中  将左节点设为根节点  知道左节点为空,这时根节点和左子树的值都放入数组中
        while (rootNode != nil) {
            [numbers addObject:@(rootNode.value)];
            
            [inorderArray addObject:rootNode];
            rootNode = rootNode.leftNode;
        }
        
        ///取出数组的最上面的值,开始读取右子树
        rootNode = [inorderArray lastObject];
        [inorderArray removeLastObject];

        rootNode = rootNode.rightNode;
    }
    
    return numbers;
}




/// 中序遍历 迭代
/// @param rootNode  根节点
- (NSArray *)inorderTraversal2:(BinaryTreeNode *)rootNode
{
    if (rootNode == nil) {
        return @[];
    }
    
    NSMutableArray *inorderArray = [NSMutableArray array];
    
    NSMutableArray *numbers = [NSMutableArray array];
    
    while (rootNode != nil || inorderArray.count > 0) {
        
        //判断节点是否为空    不为空放入数组中    然后将左节点设为跟节点   再次便利
        while (rootNode != nil) {
            [inorderArray addObject:rootNode];
            rootNode = rootNode.leftNode;
        }
        
        //最后加入的左节点 也就是最左的节点 这个节点没有左节点,下面该访问右节点
        rootNode = [inorderArray lastObject];
        [inorderArray removeLastObject];
        [numbers addObject:@(rootNode.value)];
        
        //将右节点作为跟节点 这里需要判断有无左节点
        rootNode = rootNode.rightNode;
    }
    
    return numbers;
}


/// 后序遍历 迭代
/// @param rootNode  根节点
- (NSArray *)postorderTraversal2:(BinaryTreeNode *)rootNode
{
    if (rootNode == nil) {
        return @[];
    }
    
    NSMutableArray *inorderArray = [NSMutableArray array];
    
    NSMutableArray *numbers = [NSMutableArray array];
    
    while (rootNode != nil || inorderArray.count > 0) {
        
        while (rootNode != nil) {
            [numbers addObject:@(rootNode.value)];
            
            [inorderArray addObject:rootNode];
            rootNode = rootNode.rightNode;
        }
        
        rootNode = [inorderArray lastObject];
        [inorderArray removeLastObject];

        rootNode = rootNode.leftNode;
    }
    
    NSMutableArray *newNumbers = [NSMutableArray array];
    for (NSInteger i = numbers.count-1; i>=0; i--) {
        [newNumbers addObject:numbers[i]];
    }
    
    return newNumbers;
}


/// 层序遍历 迭代
/// @param rootNode  根节点
- (NSArray *)levelOrder:(BinaryTreeNode *)rootNode
{
    if (rootNode == nil) {
        return @[];
    }
    
    NSMutableArray *dataArray = [NSMutableArray array];
    
    [dataArray addObject:rootNode];
    
    NSMutableArray *numbers = [NSMutableArray array];
    
    NSMutableArray *values = [NSMutableArray array];
    
    NSInteger count = dataArray.count;
    
    while (dataArray.count > 0) {
        
        BinaryTreeNode *node = [dataArray firstObject];
        [values addObject:@(node.value)];
        count--;
        [dataArray removeObjectAtIndex:0];
        
        if (node.leftNode) {
            [dataArray addObject:node.leftNode];
        }
        
        if (node.rightNode) {
            [dataArray addObject:node.rightNode];
        }
        
        if (count == 0) {
            count = dataArray.count;
            [numbers addObject:values];
            values = [NSMutableArray array];
        }
    }
    
    return numbers;
}

inorderArray 这里表示队列 每次取出最后一个 后进先出
numbers 这里表示节点

重点是先将根节点 和 左节点 加入数组中,直到找到最左的节点
开始取出
取出后 在将右节点加入

验证

//    144. 二叉树的前序遍历 https://leetcode-cn.com/problems/binary-tree-preorder-traversal/
//    94. 二叉树的中序遍历 https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
//    145. 二叉树的后序遍历 https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
//    102. 二叉树的层序遍历 https://leetcode-cn.com/problems/binary-tree-level-order-traversal/
    
    {
        //    输入:root = [1,null,2,3]
        //    前序遍历 输出:[1,2,3]
        //    中序遍历 输出:[1,3,2]
        //    后序遍历 输出:[3,2,1]
        //    层次遍历 输出:[[1],[2],[3]]
        
        
        BinaryTreeNode *node1 = [[BinaryTreeNode alloc] init];
        node1.value = 1;
        
        BinaryTreeNode *node2 = [[BinaryTreeNode alloc] init];
        node2.value = 2;
        
        BinaryTreeNode *node3 = [[BinaryTreeNode alloc] init];
        node3.value = 3;
        
        
        node1.rightNode = node2;
        node2.leftNode = node3;
        
        ///前序遍历
        NSArray *preorderArray1 = [self preorderTraversal1:node1];
        NSLog(@"preorderArray1 is %@",preorderArray1);
        NSArray *preorderArray2 = [self preorderTraversal2:node1];
        NSLog(@"preorderArray2 is %@",preorderArray2);
        
        ///中序遍历
        NSArray *inorderArray1 = [self inorderTraversal1:node1];
        NSLog(@"inorderArray1 is %@",inorderArray1);
        NSArray *inorderArray2 = [self inorderTraversal2:node1];
        NSLog(@"inorderArray2 is %@",inorderArray2);
        
        
        ///后序遍历
        NSArray *postorderArray1 = [self postorderTraversal1:node1];
        NSLog(@"postorderArray1 is %@",postorderArray1);
        NSArray *postorderArray2 = [self postorderTraversal2:node1];
        NSLog(@"postorderArray2 is %@",postorderArray2);
        
        ///层次遍历
        NSArray *levelOrderArray1 = [self levelOrder:node1];
        NSLog(@"levelOrderArray1 is %@",levelOrderArray1);

    }
    
    {
//        输入:root = []
//        四大遍历 输出:[]
        
        
        ///前序遍历
        NSArray *preorderArray1 = [self preorderTraversal1:nil];
        NSLog(@"preorderArray1 is %@",preorderArray1);
        NSArray *preorderArray2 = [self preorderTraversal2:nil];
        NSLog(@"preorderArray2 is %@",preorderArray2);
        
        ///中序遍历
        NSArray *inorderArray1 = [self inorderTraversal1:nil];
        NSLog(@"inorderArray1 is %@",inorderArray1);
        NSArray *inorderArray2 = [self inorderTraversal2:nil];
        NSLog(@"inorderArray2 is %@",inorderArray2);
        
        ///后序遍历
        NSArray *postorderArray1 = [self postorderTraversal1:nil];
        NSLog(@"postorderArray1 is %@",postorderArray1);
        NSArray *postorderArray2 = [self postorderTraversal2:nil];
        NSLog(@"postorderArray2 is %@",postorderArray2);
        
        ///层次遍历
        NSArray *levelOrderArray1 = [self levelOrder:nil];
        NSLog(@"levelOrderArray1 is %@",levelOrderArray1);

    }
    
    {
        
//        输入:root = [1]
//        前序遍历、中序遍历、后序遍历 输出:[1]
//        层次遍历 输出:[[1]]
        
        BinaryTreeNode *node1 = [[BinaryTreeNode alloc] init];
        node1.value = 1;
        
        
        ///前序遍历
        NSArray *preorderArray1 = [self preorderTraversal1:node1];
        NSLog(@"preorderArray1 is %@",preorderArray1);
        NSArray *preorderArray2 = [self preorderTraversal1:node1];
        NSLog(@"preorderArray2 is %@",preorderArray2);
        
        ///中序遍历
        NSArray *inorderArray1 = [self inorderTraversal1:node1];
        NSLog(@"inorderArray1 is %@",inorderArray1);
        NSArray *inorderArray2 = [self inorderTraversal2:node1];
        NSLog(@"inorderArray2 is %@",inorderArray2);
        
        ///后序遍历
        NSArray *postorderArray1 = [self postorderTraversal1:node1];
        NSLog(@"postorderArray1 is %@",postorderArray1);
        NSArray *postorderArray2 = [self postorderTraversal2:node1];
        NSLog(@"postorderArray2 is %@",postorderArray2);
        
        ///层次遍历
        NSArray *levelOrderArray1 = [self levelOrder:node1];
        NSLog(@"levelOrderArray1 is %@",levelOrderArray1);
        
    }
    
    {
//        输入:root = [1,2]
//        前序遍历  输出:[1,2]
//        中序遍历  输出: [2,1]
//        后序遍历  输出:[2,1]
//        层次遍历  输出:[[1],[2]]

        BinaryTreeNode *node1 = [[BinaryTreeNode alloc] init];
        node1.value = 1;
        
        BinaryTreeNode *node2 = [[BinaryTreeNode alloc] init];
        node2.value = 2;
        
        node1.leftNode = node2;
        
        ///前序遍历
        NSArray *preorderArray1 = [self preorderTraversal1:node1];
        NSLog(@"preorderArray1 is %@",preorderArray1);
        NSArray *preorderArray2 = [self preorderTraversal1:node1];
        NSLog(@"preorderArray2 is %@",preorderArray2);
        
        ///中序遍历
        NSArray *inorderArray1 = [self inorderTraversal1:node1];
        NSLog(@"inorderArray1 is %@",inorderArray1);
        NSArray *inorderArray2 = [self inorderTraversal2:node1];
        NSLog(@"inorderArray2 is %@",inorderArray2);
        
        ///后序遍历
        NSArray *postorderArray1 = [self postorderTraversal1:node1];
        NSLog(@"postorderArray1 is %@",postorderArray1);
        NSArray *postorderArray2 = [self postorderTraversal2:node1];
        NSLog(@"postorderArray2 is %@",postorderArray2);
        
        ///层次遍历
        NSArray *levelOrderArray1 = [self levelOrder:node1];
        NSLog(@"levelOrderArray1 is %@",levelOrderArray1);
        
    }
    
    {
//        输入:root = [1,null,2]
//        前序遍历、中序遍历 输出:[1,2]
//        后序遍历 输出:[2,1]
//        层次遍历 输出:[[1],[2]]
        
        BinaryTreeNode *node1 = [[BinaryTreeNode alloc] init];
        node1.value = 1;
        
        BinaryTreeNode *node2 = [[BinaryTreeNode alloc] init];
        node2.value = 2;
        
        node1.rightNode = node2;
        
        ///前序遍历
        NSArray *preorderArray1 = [self preorderTraversal1:node1];
        NSLog(@"preorderArray1 is %@",preorderArray1);
        NSArray *preorderArray2 = [self preorderTraversal1:node1];
        NSLog(@"preorderArray2 is %@",preorderArray2);
        
        ///中序遍历
        NSArray *inorderArray1 = [self inorderTraversal1:node1];
        NSLog(@"inorderArray1 is %@",inorderArray1);
        NSArray *inorderArray2 = [self inorderTraversal2:node1];
        NSLog(@"inorderArray2 is %@",inorderArray2);
        
        ///后序遍历
        NSArray *postorderArray1 = [self postorderTraversal1:node1];
        NSLog(@"postorderArray1 is %@",postorderArray1);
        NSArray *postorderArray2 = [self postorderTraversal2:node1];
        NSLog(@"postorderArray2 is %@",postorderArray2);
        
        ///层次遍历
        NSArray *levelOrderArray1 = [self levelOrder:node1];
        NSLog(@"levelOrderArray1 is %@",levelOrderArray1);
        
    }
    
    {
//        输入:root = [3,9,20,null,null,15,7]
//        前序遍历 输出:[3,9,20,15,7]
//        中序遍历 输出:[9,3,15,20,7]
//        后序遍历 输出:[9,15,7,20,3]
//        层次遍历输出:[[3],[9,20],[15,7]]
        
        BinaryTreeNode *node1 = [[BinaryTreeNode alloc] init];
        node1.value = 3;
        
        BinaryTreeNode *node2 = [[BinaryTreeNode alloc] init];
        node2.value = 9;
        
        BinaryTreeNode *node3 = [[BinaryTreeNode alloc] init];
        node3.value = 20;
        
        node1.leftNode = node2;
        node1.rightNode = node3;

        BinaryTreeNode *node4 = [[BinaryTreeNode alloc] init];
        node4.value = 15;
        
        BinaryTreeNode *node5 = [[BinaryTreeNode alloc] init];
        node5.value = 7;
        
        node3.leftNode = node4;
        node3.rightNode = node5;

        ///前序遍历
        NSArray *preorderArray1 = [self preorderTraversal1:node1];
        NSLog(@"preorderArray1 is %@",preorderArray1);
        NSArray *preorderArray2 = [self preorderTraversal1:node1];
        NSLog(@"preorderArray2 is %@",preorderArray2);
        
        ///中序遍历
        NSArray *inorderArray1 = [self inorderTraversal1:node1];
        NSLog(@"inorderArray1 is %@",inorderArray1);
        NSArray *inorderArray2 = [self inorderTraversal2:node1];
        NSLog(@"inorderArray2 is %@",inorderArray2);
        
        ///后序遍历
        NSArray *postorderArray1 = [self postorderTraversal1:node1];
        NSLog(@"postorderArray1 is %@",postorderArray1);
        NSArray *postorderArray2 = [self postorderTraversal2:node1];
        NSLog(@"postorderArray2 is %@",postorderArray2);
        
        ///层次遍历
        NSArray *levelOrderArray1 = [self levelOrder:node1];
        NSLog(@"levelOrderArray1 is %@",levelOrderArray1);
        
    }
    
    {
        
        BinaryTreeNode *node1 = [BinaryTreeManager createTreeWithValues:@[@6,@1,@2,@3,@4,@5,@7,@8,@9,@10,@11]];
        
        ///前序遍历
        NSArray *preorderArray1 = [self preorderTraversal1:node1];
        NSLog(@"preorderArray1 is %@",preorderArray1);
        NSArray *preorderArray2 = [self preorderTraversal1:node1];
        NSLog(@"preorderArray2 is %@",preorderArray2);
        
        ///中序遍历
        NSArray *inorderArray1 = [self inorderTraversal1:node1];
        NSLog(@"inorderArray1 is %@",inorderArray1);
        NSArray *inorderArray2 = [self inorderTraversal2:node1];
        NSLog(@"inorderArray2 is %@",inorderArray2);
        
        ///后序遍历
        NSArray *postorderArray1 = [self postorderTraversal1:node1];
        NSLog(@"postorderArray1 is %@",postorderArray1);
        NSArray *postorderArray2 = [self postorderTraversal2:node1];
        NSLog(@"postorderArray2 is %@",postorderArray2);
        
        ///层次遍历
        NSArray *levelOrderArray1 = [self levelOrder:node1];
        NSLog(@"levelOrderArray1 is %@",levelOrderArray1);
        
    }

你可能感兴趣的:(leetcode 二叉树的前序、中序、后序、层次遍历(OC))