zoukankan      html  css  js  c++  java
  • 剑指offer-面试题8-二叉树的下一个节点-二叉树

    /*
    题目:
    	给定一棵二叉树和其中一个节点,找出中序遍历的下一个节点。
    */
    /*
    思路:
    	两种情况:
    	节点存在右子树:节点右子树的最左节点;
    	节点不存在右子树,节点向上一直找父节点或祖父节点,直到其父节点或祖父节点为其双亲节点的左子树,则next节点为父节点或祖父节点,若没有找到,则无next节点。
    */
    struct TreeNode{
    	int value;
    	TreeNode* left;
    	TreeNode* right;
    	TreeNode* parent;
    }
    
    BinaryTreeNode* GetNext(BinaryTreeNode* pNode){
    	if(pNode == null) return null;
    	
    	BinaryTreeNode* targetNode = pNode;
    	if(targetNode->right != null){
    		targetNode = targetNode->right;
    		while(targetNode->left != null){
    			targetNode = targetNode->left;
    		}	
    	}else if(targetNode->parent != null){
    		targetNode = targetNode->parent;
    		while(targetNode != null && targetNode->parent->right == target){
    			targetNode = targetNode->parent;
    		}
    		if(targetNode != null){
    			targetNode = targetNode->parent;
    		}
    	
    	return targetNode;
    }
    		
    

       

  • 相关阅读:
    ffserver搭建服务器
    socketconnect函数详解
    ffmpeg 发送媒体流
    Android—Socket编程
    流媒体客户端的结构与原理浅析
    杭电分类:大数
    各种计时函数
    UVA465:Overflow
    杭电:sort
    CODING常见错误原因
  • 原文地址:https://www.cnblogs.com/buaaZhhx/p/11822749.html
Copyright © 2011-2022 走看看